package cn.com.haopy.dataes.hbase;


import cn.com.haopy.dataes.db.entity.service.HRecipel;
import cn.com.haopy.dataes.db.service.service.HRecipelService;
import cn.com.haopy.dataes.hbase.service.base.HBaseAdminService;
import cn.com.haopy.dataes.hbase.service.user.StudentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.TemporalField;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class HbaseTestAdmin {

    private static Long maxTime = 999999999999999999l;
    /*@Autowired
    private IHBaseAdminTemplate adminTemplate;*/

  /*  @Autowired
    private HbaseTemplate hbaseTemplate;*/

 /*   @Autowired
    private StudentService studentService;*/

    @Autowired
    HBaseAdminService hBaseAdminService;

    @Autowired
    private HRecipelService hRecipelService;


    @Test
    public void testMain() throws Exception {
        String nameSpace = "healthy";
        String table = "h_recipel";
        //studentService.selectList("100","200") ;
        //Boolean bool = hBaseAdminService.creatSpace("healthy");
        //System.out.println("createSpaceResult:" + bool);

        //System.out.println(hBaseAdminService.getAllSpaceNames());
        List<String> familys = new ArrayList<>();
        familys.add("base");
        familys.add("store");
        familys.add("employee");
        familys.add("doctor");
        familys.add("pharms");
        familys.add("customer");
        familys.add("allocate");
        familys.add("review");

        //System.out.println(hBaseAdminService.createTable(nameSpace, table, familys));

        //hBaseAdminService.putData(nameSpace,table,"100000000","base","id","1");
        //hBaseAdminService.putData(nameSpace,table,"100000000","base","interrogation_id","222");
        //hBaseAdminService.putData(nameSpace,table,"100000000","doctor","doct_id","222");

        //hBaseAdminService.dropTable("default", table);

        //System.out.println(hBaseAdminService.getDataRowKey(nameSpace,table,"100000000"));

    }


    /**
     * 同步 处方表
     *
     * @throws Exception
     */
    @Test
    public void syncRecipel() throws Exception {
        Integer pageNo = 1;
        Integer pageSize = 10000;
        Long pageTotal = 0l;
        Page<HRecipel> pages = hRecipelService.querySelective(pageNo, pageSize, "create_date", "asc");
        pageTotal = pages.getPages();
        List<HRecipel> listRecord = pages.getRecords();
        saveRecipel(listRecord);
       /* for (Integer i = 2; i <= pageTotal; i++) {
            pageNo = i;
            Page<HRecipel> pageNew = hRecipelService.querySelective(pageNo, pageSize, "create_date", "asc");
            saveRecipel(pageNew.getRecords());
        }*/

    }


    public void saveRecipel(List<HRecipel> list) {
        String nameSpace = "healthy";
        String table = "h_recipel";

        String base = "base";
        String store = "store";
        String employee = "employee";
        String doctor = "doctor";
        String pharms = "pharms";
        String customer = "customer";
        String allocate = "allocate";
        String review = "review";

        //Long time = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        //System.out.println(time);

        list.forEach(hRecipel -> {
            //long timestamp = hRecipel.getCreateDate().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
            String rowKey = hRecipel.getId();
            try {
                hBaseAdminService.putData(nameSpace, table, rowKey, base, "id", hRecipel.getId());
                hBaseAdminService.putData(nameSpace, table, rowKey, base, "create_date", hRecipel.getCreateDate() + "");

                hBaseAdminService.putData(nameSpace, table, rowKey, store, "store_code", hRecipel.getStoreCode());

                hBaseAdminService.putData(nameSpace, table, rowKey, employee, "employee_code", hRecipel.getEmployeeCode());

                hBaseAdminService.putData(nameSpace, table, rowKey, doctor, "rx_doctor", hRecipel.getRxDoctor());
                hBaseAdminService.putData(nameSpace, table, rowKey, doctor, "sign_user", hRecipel.getSignUser());
                hBaseAdminService.putData(nameSpace, table, rowKey, doctor, "sign_code", hRecipel.getSignCode());


                hBaseAdminService.putData(nameSpace, table, rowKey, pharms, "verify_code", hRecipel.getVerifyCode());
                hBaseAdminService.putData(nameSpace, table, rowKey, pharms, "verify_user", hRecipel.getVerifyUser());

                hBaseAdminService.putData(nameSpace, table, rowKey, customer, "customer_code", hRecipel.getCustomerCode());


            } catch (Exception e) {
                e.printStackTrace();
            }

        });
    }


    @Test
    public void timeRowKeyMaxTest() {
        String nameSpace = "healthy";
        String table = "h_recipel";
        List<Map<String, String>> list = hBaseAdminService.getDataLimit(nameSpace, table, null);
        System.out.println(list);
    }

    @Test
    public void timeRowKeyData() {
        System.out.println(LocalDateTime.now());
        String nameSpace = "healthy";
        String table = "h_recipel";
        Map<String, String> data = hBaseAdminService.getDataRowKey(nameSpace, table, "1169561507830525952");
        System.out.println(data);
        System.out.println(LocalDateTime.now());
    }

}
